/****************************************************************************
**
** This file is part of the KD Chart library.
**
** SPDX-FileCopyrightText: 2001-2023 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
**
** SPDX-License-Identifier: MIT
**
****************************************************************************/

#include <QApplication>
#include <QDateTime>
#include <QDebug>
#include <QLabel>
#include <QMenu>
#include <QPainter>
#include <QPixmap>
#include <QPixmapCache>
#include <QPrintDialog>
#include <QPrinter>
#include <QPushButton>
#include <QSlider>
#include <QStandardItemModel>
#include <QVBoxLayout>

#include <KDGanttConstraintModel>
#include <KDGanttDateTimeGrid>
#include <KDGanttGraphicsView>
#include <KDGanttView>

#include "QFile"
#include <QApplication>
#include <QTextStream>


// 根据自己的需要添加库文件
#include "kdganttitemdelegate.h"


class MyStandardItem : public QStandardItem
{
public:
    MyStandardItem(const QVariant &v)
        : QStandardItem()
    {
        setData(v, Qt::DisplayRole);
        // setFlags( flags() & ~Qt::ItemIsEditable );
    }
    MyStandardItem(const QString &v)
        : QStandardItem()
    {
        setData(v, Qt::DisplayRole);
        // setFlags( flags() & ~Qt::ItemIsEditable );
    }
    MyStandardItem(const QDateTime &dt, int role)
        : QStandardItem()
    {
        setData(QVariant::fromValue(dt), role);
    }
};

class MyWidget : public QWidget
{
    Q_OBJECT
public:
    MyWidget()
        : QWidget(0)
    {
        // 准备好每个item的起始、结束时间
        qDebug()<<QDateTime::currentDateTime();

        // 将时间解析成 QDateTime
        QString Tasktime_test="2023/12/20/09:50:31-2023/12/21/13:55:31";
        QString start_time = Tasktime_test.left(19);
        QString end_time = Tasktime_test.right(19);
        QString format = "yyyy/MM/dd/hh:mm:ss";
        QDateTime start_dateTime = QDateTime::fromString(start_time, format);
        QDateTime end_dateTime = QDateTime::fromString(end_time, format);
        qDebug()<< start_dateTime ;
        qDebug()<< end_dateTime ;



        qDebug() << "Building data";
        // proxyModel.setSourceModel( &model );
        for (int h = 1; h < 2; ++h) {
            QStandardItem *topitem = new MyStandardItem(QString::fromLatin1("task %1").arg(h));
            for (int i = 1; i < 3; ++i) {
                QStandardItem *item = new MyStandardItem(QString::fromLatin1("plane %1").arg(i));
                for (int j = 1; j < 3; ++j)
                {
                    QStandardItem *routeitem = new MyStandardItem(QString::fromLatin1("route %1").arg(j));
                    for (int z = 1; z < 4; ++z)
                    {
                        routeitem->appendRow(QList<QStandardItem *>()
                                        << new MyStandardItem(QString::fromLatin1("stage %1").arg(z))
                                        << new MyStandardItem(KDGantt::TypeTask)
                                        << new MyStandardItem(start_dateTime.addDays(i+j+z), KDGantt::StartTimeRole)
                                        << new MyStandardItem(start_dateTime.addDays(i+j+z+7), KDGantt::EndTimeRole)
                                        << new MyStandardItem(QString())
                                        << new MyStandardItem(QString("阶段信息")));  //单纯显示数值 也可以直接放数字MyStandardItem(1)

//                        routeitem->appendRow(QList<QStandardItem *>()
//                                        << new MyStandardItem(QString::fromLatin1("Event"))
//                                        << new MyStandardItem(KDGantt::TypeEvent)
//                                        << new MyStandardItem(QDateTime::currentDateTime(), KDGantt::StartTimeRole)
//                                        << new MyStandardItem(QDateTime(), KDGantt::EndTimeRole)
//                                        << new MyStandardItem(QString())
//                                        << new MyStandardItem(QString("节点信息")));
                    }

                    item->appendRow(QList<QStandardItem *>()
                                         << new MyStandardItem(QString::fromLatin1("route_start_Event %1").arg(j))
                                         << new MyStandardItem(KDGantt::TypeEvent)
                                         << new MyStandardItem(QDateTime::currentDateTime().addDays(i+j+1), KDGantt::StartTimeRole)
                                         << new MyStandardItem(QDateTime(), KDGantt::EndTimeRole)
                                         << new MyStandardItem(QString())
                                         << new MyStandardItem(QString("节点信息")));


                    item->appendRow(QList<QStandardItem *>()
                                       << routeitem
                                       << new MyStandardItem(KDGantt::TypeMulti)
                                       << new MyStandardItem(QString())
                                       << new MyStandardItem(QString())
                                       << new MyStandardItem(QString("航线信息"))
                                       << new MyStandardItem(QString()));
                }

                topitem->appendRow(QList<QStandardItem *>()
                                   << item
                                   << new MyStandardItem(KDGantt::TypeMulti)
                                   << new MyStandardItem(QString())
                                   << new MyStandardItem(QString("飞机信息"))
                                   << new MyStandardItem(QString())
                                   << new MyStandardItem(QString()));
            }
            model.appendRow(QList<QStandardItem *>()
                            << topitem
                            << new MyStandardItem(KDGantt::TypeMulti /*Summary*/)
                            << new MyStandardItem(QString("任务信息"))
                            << new MyStandardItem(QString())
                            << new MyStandardItem(QString())
                            << new MyStandardItem(QString()));
        }


        qDebug() << "Creating view";
        slider.setOrientation(Qt::Horizontal);  // 滑动条
        slider.setRange(1, 10000);  // 滑动条的总长度值
        slider.setValue(100);   // 滑动条的初始值

        auto *l = new QVBoxLayout(this); // 使用垂直布局，先放置甘特图的view+滑动条+两个Putthon
        l->addWidget(&view);
        l->addWidget(&slider);


        // 颜色设置（记得头文件要加入对应的h文件）
        QBrush myTaskBrush( Qt::green, Qt::Dense3Pattern);
        QBrush myEventBrush( Qt::red );  //Qt::NoBrush
        QBrush mySummaryBrush( Qt::yellow );
        view.graphicsView()->itemDelegate()->setDefaultBrush( KDGantt::TypeTask,myTaskBrush );
        view.graphicsView()->itemDelegate()->setDefaultBrush( KDGantt::TypeEvent,myEventBrush );
        view.graphicsView()->itemDelegate()->setDefaultBrush( KDGantt::TypeSummary,mySummaryBrush );


        grid.setScale( KDGantt::DateTimeGrid::ScaleHour );  //设置尺度  ScaleDay ScaleHour


        grid.setStartDateTime(QDateTime::currentDateTime().addDays(-1)); //起始时间设置为当前时间-3天
        grid.setDayWidth(100);  // 每天的宽度
        // grid.setNoInformationBrush( Qt::NoBrush );
        view.setGrid(&grid);
        view.setModel(&model);

        connect(&slider, SIGNAL(valueChanged(int)),
                this, SLOT(slotZoom(int)));

        auto *pb1 = new QPushButton(tr("Print Preview..."));
        l->addWidget(pb1);
        connect(pb1, SIGNAL(clicked()), this, SLOT(slotPrintPreview()));
    }

public slots:
    void slotZoom(int z)
    {
        grid.setDayWidth(z);
    }

    void slotPrintPreview()   // 将grid的图放大显示
    {
        QPixmap pix(1000, 200);
        pix.fill(Qt::white);
        QPainter painter(&pix);
        view.print(&painter, pix.rect());
        painter.end();
        auto *label = new QLabel;
        label->setPixmap(pix);
        label->show();
    }


private:
    KDGantt::View view;
    KDGantt::DateTimeGrid grid;
    QSlider slider;
    QStandardItemModel model;
};

int main(int argc, char **argv)
{
    QApplication app(argc, argv);
    QPixmapCache::setCacheLimit(30 * 1024);

    QString content;
    QFile file(":/qss/YkbRes/default.qss");
    file.open(QFile::ReadOnly | QIODevice::Text);
    if(file.isOpen())
    {
        QTextStream in(&file);
        content = in.readAll();
        file.close();
        qApp->setStyleSheet(content);
    }

    MyWidget w;
    w.show();

    return app.exec();
}

#include "main.moc"
